jQuery(document).ready(function($){
   

    $('input[name="payment_opition[]"]').on('click',function(){
          
          var ival = $(this).val();
          $('.box-payment-container-options').html('<div class="grid-loading"></div>');
          if(ival == 'cartao'){

          	    $.ajax({
          	    	url:url_site + '/payments/getOptions/',
          	    	beforeSend:function(){
                        
           	    	},
          	    	success:function(data){


          	    		var html  =  form_open(url_site +'payments/process','id="formSubmiPayment"') + 
          	    		              '<div class="form-group">' +
          	    		              data +
          	    		              '</div>' +
          	    		              '<div class="form-group">' +
                                      form_submit('','Pagar','class="btn btn-primary"') +
          	    		              '</div>'
          	    		              ;

                        $('.box-payment-container-options').html(html);
                         is_submit_form();
                         is_change_credit_payment()
          	    	}
          	    });
          }else{
                 
                 var html  =  form_open(url_site +'payments/process','id="formSubmiPaymentBoleto"') + 
          	    		              '<div class="form-group">' +
          	    		              form_dropdown('payment_boleto',{'':'Selecione uma opção','bradesco':'Boleto Bancário'},false,'class="form-control"') +
          	    		              '</div>' +
                                
          	    		              '<div class="form-group">' +
                                      form_submit('','Pagar','class="btn btn-primary"') +
          	    		              '</div>'
          	    		              ;

                        $('.box-payment-container-options').html(html);
                        is_submit_form_boleto();


          }


    });
   //box-payment-container-options


   function  is_submit_form(){
                
                $('#formSubmiPayment').submit(function(e){
                     var objForm = this,markup = '<div id="mdpInputs">';
                         
                     $('.grid-payments').find('input').each(function(){
                              markup += form_hidden($(this).attr('name'),$(this).val());


                     });
                     markup +='</div>';
                     $('#mdpInputs').remove();  
                     $(objForm).append(markup);
                      var ival = $(this).find('option:selected').val();
                            if(ival == '' || ival == false){
                            	 alert('Selecione um opção de pagamento');
                            	 e.preventDefault();
                            }

                });


   }

   function  is_submit_form_boleto(){
                
                $('#formSubmiPaymentBoleto').submit(function(e){
                     var objForm = this,data = {},

                     markup = 
                      '<div class="modal fade boleto" id="myModal" tabindex="-1"  role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">' +
                      '<div class="modal-dialog" >'+
                      '<div class="modal-content">'+
                      '<div class="modal-header">'+
                      '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' +
                      '<h4 class="modal-title" id="myModalLabel">Boleto Bancário</h4>' +
                      '</div>' +
                      '<div class="modal-body">' +
       
                      '</div>' +
                      '<div class="modal-footer"> <a target="_blank" role="printBoleto" href="javascript:void();" class="link "><i class="glyphicon glyphicon-print"></i> Imprimir</a>' +
                      '</div>' +
                      '</div>' +
                      '</div>'+
                      '</div>',
                     form_html  =  form_open(url_site +'payments/process','id="formSubmiPayment"') + 
                                  '<div class="form-group">' +
                                  '</div>' +
                                   form_close()
                                  ;

                       


                     e.preventDefault();
                     $('.grid-payments').find('input').each(function(){
                              data[$(this).attr('name')] = $(this).val();


                     });
                      
                      
                     
                      var ival = $(this).find('option:selected').val();
                            if(ival == '' || ival == false){
                               alert('Selecione um opção de pagamento');
                              
                            }else{

                                $.ajax({
                                url:url_site + '/payments/process/',
                                type:'post',
                                data:data,
                        beforeSend:function(){
                              $(markup).appendTo('body').modal();
                              $('#myModal .modal-body').html('<div class="grid-loading"></div>');
                        },
                          success:function(data){
                                  $('#myModal .modal-body').html(data);
                                  $('a[role="showbox"]').on('click',function(){$('.box-instrucoes').toggle();});
                                   $('#myModal .modal-body').append(form_html);
                                     
                                 
                                  is_submit_form();
                                   $('a[role="printBoleto"]').click(function(e){
                                           $('#formSubmiPayment').trigger('submit');
                                                
                                  });

                          }

                            });

                            }

                });


   }

   function is_change_credit_payment(){
              
            $('#formSubmiPayment select[name="payment_credito"]').change(function(){

                         var ival = $(this).val(), 
                                el =  $('[payment="total"]'),
                                total = el.attr('data-total-value'),
                                 itotal  = el.text()
                               ;
                              
                              if(ival != ''){

                                 $.ajax({
                                  url:url_site +'payments/getTaxaPayment',
                                  dataType:'json',
                                  type:'post',
                                  data:{max_payment:ival},
                                  success:function(data){
                                      if(data.error == false){
                                             var calc = ((parseFloat(total) * parseFloat(data.tax))/100) + parseFloat(total);
                                             el.text('Total R$ '+ number_format(calc,2,',','.'));
                                      } 
                                  }
                                 });
                                    
                              }



            });  

   }

});